package cn.scs.component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.function.Function;

// 周期任务类 用于指定线程开启周期响应执行的函数线程
public class CycleTask extends Thread {
    private static List<CycleTask> queue = new ArrayList<>();   // 储存当前进程内 所有CycleTask

    private long timePointer;    // 上次执行的时间记录
    private int cycleTime;  // 循环周期 秒
    private String name;    // 任务名称
    private Runnable Runner;
    // 初始化
    public CycleTask(String taskname, int cycletime, Runnable function) {
        this.cycleTime = cycletime;
        this.name = taskname;
        this.Runner = function;
        queue.add(this);    // 推入队列
    }
    // run
    @Override
    public void run() {
        try {
            while (true) {
                sleep(cycleTime*1000);
                this.Runner.run();
                timePointer = System.currentTimeMillis()/1000;   // 更新最后执行时间
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
